﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Data.Helpers;
using DevExpress.Data.PLinq.Helpers;
using DevExpress.Spreadsheet;
using DevExpress.Web;
using DevExpress.Web.ASPxSpreadsheet;
using SpreadSheetFramework.TemplateExcel;
using SpreadSheetFramework.Util;
namespace SpreadSheetFramework
{
    public partial class UcSpreadSheet : System.Web.UI.UserControl
    {
        protected const string TXT_RESULT_SESSION="txtResult";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["t"] != null)
                {
                    Session[FarmeworkConst.TEMPLE_SESSION] = null;
                    SpreadSheetTemplateType type;
                    if (Enum.TryParse(Request.QueryString["t"], true, out type))
                    {
                        if (Enum.IsDefined(typeof (SpreadSheetTemplateType), type))
                        {
                            switch (type)
                            {
                                case SpreadSheetTemplateType.Base:
                                    InitSheet(new TestTemplate());
                                    break;
                                case SpreadSheetTemplateType.UnconfinedCompressionTest:
                                    InitSheet(new UnconfinedCompressionTest());
                                    break;
                                case SpreadSheetTemplateType.QuTemplate:
                                    InitSheet(new QuTemplate());
                                    break;
                            }
                        }
                    }
                }

            }
        }

        protected void InitSheet(SpreadSheetTemplate template)
        {
            if (template != null)
            {
                Session[FarmeworkConst.TEMPLE_SESSION] = template;
                var templateSheet=new ASPxSpreadsheet();
                template.GenTemplate(spreadsheet,templateSheet);
            }
        }

        #region Proc client  event
        protected void spreadsheet_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e)
        {
            var temp = Session[FarmeworkConst.TEMPLE_SESSION] as SpreadSheetTemplate;
            if (temp != null)
            {
               temp.SpreadSheetCallbackEvent(spreadsheet,sender,e);
            }
        }

        #endregion

        protected void btCalculte_Onclick(object sender, EventArgs e)
        {
            var temp = Session[FarmeworkConst.TEMPLE_SESSION] as SpreadSheetTemplate;
            var txtResult= Session[TXT_RESULT_SESSION] as ASPxGridView;
            if (temp != null)
            {
                temp.OnRequestCalculate(spreadsheet, txtResult);
            }
        }

        protected void gridError_Onload(object sender, EventArgs e)
        {
            Session[TXT_RESULT_SESSION] = sender;
        }

        protected void btResetInput_Onclick(object sender, EventArgs e)
        {
            var temp = Session[FarmeworkConst.TEMPLE_SESSION] as SpreadSheetTemplate;
            if (temp != null)
            {
                temp.ClearDictErr();
                var txtResult = Session[TXT_RESULT_SESSION] as ASPxGridView;
                if (txtResult != null)
                {
                    var dt = new DataTable();
                    dt.Columns.Add("Error");
                    dt.Columns.Add("RangeInfo");
                    txtResult.DataSource = dt;
                    txtResult.DataBind();
                }
            }
        }

        protected void btDownload_Onclick(object sender, EventArgs e)
        {
            var temp = Session[FarmeworkConst.TEMPLE_SESSION] as SpreadSheetTemplate;
            if (temp != null)
            {
                var st = new MemoryStream();
                spreadsheet.Document.SaveDocument(st, DocumentFormat.Xlsx);
                Response.Clear();
                Response.ContentType = "application/force-download";
                Response.AddHeader("content-disposition", "attachment; filename=my_" + temp.Type + "_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".xlsx");
                Response.BinaryWrite(st.ToArray());
                Response.End();
            }
        }
    }
}